package user;

import java.sql.*;


public class DisplayOrderForm
{
	
	public static String getOrderFormInfo(String OrderStatus)
	{
		DBConnector db = DBConnector.getDBConnector();		
		//String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		String statement = "select form_ID, cus_name, form_time, form_state from order_form, customer where customer.cus_ID = order_form.cus_ID and form_state = '" + OrderStatus + "'";
		db.prepareStatement(statement);
		//ResultSet FoodInfo = db.queryPstmt();
		
		StringBuffer sBuf = new StringBuffer();
		sBuf.append(OrderStatus);
		//sBuf.append("<table width = 100% align = center border = 1><tr>");
		return sBuf.toString();	
	}	
	
	//Get called for displaying the order form lists (as well as the available order states) in administrator account
	public static String displayOrderFormList(String OrderStatus, String CusID, String CusName)
	{
		StringBuffer sBuf = new StringBuffer();
		
		sBuf.append("<tr bgColor=#cccccc><td>order form ID</td><td>customer name</td><td>order time</td><td>order status</td><td>update status</td></tr>");

		DBConnector db = DBConnector.getDBConnector();
		if (OrderStatus != null) {
			String DeliveryState[] = {
					"<option value='not cooked yet'>not cooked yet</option>",
					"<option value='cooking'>cooking</option>",
					"<option value='ready to deliver'>ready to deliver</option>",
					"<option value='delivering'>delivering</option>",
					"<option value='done'>done</option>" };
			String statement = "select form_ID, cus_name, form_time, form_state from order_form, customer where customer.cus_ID = order_form.cus_ID ";
			if (!OrderStatus.equals("any"))
				statement += "and form_state = '" + OrderStatus + "' ";
			if (CusID.length() > 0)
				statement += "and customer.cus_ID = " + CusID + " ";
			if (CusName.length() > 0)
				statement += "and cus_name like '%" + CusName + "%' ";
			statement += "order by form_time desc";

			try{
				db.prepareStatement(statement);
				ResultSet OrderInfo = db.queryPstmt();
	
				if (OrderInfo != null) {
					while (OrderInfo.next()) {
						sBuf.append("<tr>");
						sBuf.append("<td>" + OrderInfo.getString(1)
								+ "</td>");
						sBuf.append("<td>" + OrderInfo.getString(2)
								+ "</td>");
						sBuf.append("<td>" + OrderInfo.getString(3)
								+ "</td>");
						sBuf.append("<td>" + OrderInfo.getString(4)
								+ "</td>");
	
						String status = OrderInfo.getString(4);
	
						int index = 0;
						if (status.equals("done"))
							sBuf.append("<td>You cannot change the status.</td>");
						else {
							if (status.equals("not cooked yet")) {
								index = 0;
							} else if (status.equals("cooking")) {
								index = 1;
							} else if (status.equals("ready to deliver")) {
								index = 2;
							} else if (status.equals("delivering")) {
								index = 3;
							}
	
							String OptionItems = "";
							for (int i = index; i < 5; i++)
								OptionItems = OptionItems
										+ DeliveryState[i];
	
							sBuf.append("<td><select name='"
									+ OrderInfo.getString(1)
									+ "' style='width:140px'>"
									+ OptionItems + "</select></td>");
						}
						sBuf.append("</tr>");
	
					}
					sBuf.append("<tr><td colspan = 5><INPUT type=submit value=submit name=SubmitUpdate></td></tr>");
				} else
					sBuf.append("<tr><td colspan = 5>There are no order forms fulfilling yoru requirement.</td></tr>");
			}catch (Exception ex) {
				System.out.println("DisplayOrderForm.displayOrderFormList exception");
			}
		}
		return sBuf.toString();
	}
	
	
	public static String displayOrderFormListForCustomer(String CusID)
	{
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();
		String AllOrderForms = "select form_ID from order_form where cus_ID = " + CusID + " order by form_time desc";
		
		try{
			db.prepareStatement(AllOrderForms);
			ResultSet AllOrderFormsResult = db.queryPstmt();
			if (AllOrderFormsResult != null)
			{
				//for all the order forms of the current customer
				while (AllOrderFormsResult.next())
				{
					String FormID = AllOrderFormsResult.getString(1);
					String PriceOfOrderForm = "select food_price, order_quantity from food, contain, order_form" +
					" where contain.food_ID = food.food_ID and contain.form_ID = order_form.form_ID and " +
					"order_form.form_ID = " + FormID;
					db.prepareStatement(PriceOfOrderForm);
					ResultSet PriceOfOrderFormResult = db.queryPstmt();
					if (PriceOfOrderFormResult != null)
					{				
						String QueryOrderForms = "select form_ID, form_time, form_state from order_form where form_ID = " + FormID;
						db.prepareStatement(QueryOrderForms);
						ResultSet QueryOrderFormsResult = db.queryPstmt();
						
						sBuf.append("<FORM name='order_form' action='customer_account.jsp' method=post>");
						sBuf.append("<tr>");
						if (QueryOrderFormsResult != null)
						{
							if (QueryOrderFormsResult.next())
							{
								sBuf.append("<td>" + QueryOrderFormsResult.getString(1) + "</td>");
								sBuf.append("<td>" + QueryOrderFormsResult.getString(2) + "</td>");
								sBuf.append("<td>" + QueryOrderFormsResult.getString(3) + "</td>");								
							}
							
							float TotalPrice = 0;
							while (PriceOfOrderFormResult.next())
							{
								TotalPrice += PriceOfOrderFormResult.getFloat(1) * PriceOfOrderFormResult.getInt(2);
							}						
							String TotalPriceString = Float.toString(TotalPrice);
							sBuf.append("<td>" + TotalPriceString + "</td>");
							
							String orderStatus = QueryOrderFormsResult.getString(3);
							sBuf.append("<td><input name = 'CheckOrderForm' type = 'radio' class = 'input' value = '" + QueryOrderFormsResult.getString(1) + "'>Check</td>");
							if (orderStatus.equals("not cooked yet") || orderStatus.equals("cooking"))
							{
								sBuf.append("<td><input name = 'CancelOrderForm' type = 'radio' class = 'input' value = '" + QueryOrderFormsResult.getString(1) + "'>Cancel</td>");
							}	
							else
								sBuf.append("<td></td>");			
							sBuf.append("<TD><INPUT type=submit value=apply name=SubmitSearchOrderForm></TD>");			
							sBuf.append("</tr>");
							sBuf.append("</FORM>");
						}						
					}					
				}				
			}
		}catch (Exception ex) {
			System.out.println("DisplayOrderForm.displayOrderFormListForCustomer exception");
		}
		return sBuf.toString();
	}
	
	//display specific details of a order form, called in customer_account
	public static String CheckOrderFormDetails(String CheckOrderForm)
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();
		String QueryOrderForm = "select receiver_name, receiver_add, receiver_phone, form_time, food_name, food_price, order_quantity  from order_form, contain, food " +
  		"where order_form.form_ID = contain.form_ID and contain.food_ID = food.food_ID and order_form.form_ID = " + CheckOrderForm;
  		
		try{
			db.prepareStatement(QueryOrderForm);
	  		ResultSet QueryOrderFormResult = db.queryPstmt();
	  		
	  		if (QueryOrderFormResult != null && QueryOrderFormResult.next())
	  		{	
	  			String receiver_name = QueryOrderFormResult.getString(1);
	  			String receiver_add = QueryOrderFormResult.getString(2);
	  			String receiver_phone = QueryOrderFormResult.getString(3);
	  			
	  			String QueryCustomerInfo = "select cus_name, cus_address, cus_phone from customer, order_form where order_form.cus_ID = customer.cus_ID and form_ID = " + CheckOrderForm;
	  			db.prepareStatement(QueryCustomerInfo);
	  			ResultSet QueryCustomerInfoResult = db.queryPstmt();
	  			if (QueryCustomerInfoResult != null && QueryCustomerInfoResult.next())
	  			{
	  				if (receiver_name == null)
	  					receiver_name = QueryCustomerInfoResult.getString(1);
	  				if (receiver_add == null)
	  					receiver_add = QueryCustomerInfoResult.getString(2);
	  				if (receiver_phone == null)
	  					receiver_phone = QueryCustomerInfoResult.getString(3);
	  			}
	  			sBuf.append("<tr><td aligh = right>Receiver Name : </td><td>" + receiver_name + "</td></tr>");
	  			sBuf.append("<tr><td aligh = right>Receiver Address: </td><td>" + receiver_add + "</td></tr>");
	  			sBuf.append("<tr><td aligh = right>Receiver Phone : </td><td>" + receiver_phone + "</td></tr>");
	  			sBuf.append("<tr><td aligh = right>Order Number : </td><td>" + CheckOrderForm + "</td></tr>");
	  			sBuf.append("<tr><td aligh = right>Order Time : </td><td>" + QueryOrderFormResult.getString(4) + "</td></tr>");
	  			sBuf.append("<tr><td><br></td><tr>");
	  			
	  			sBuf.append("<tr><td aligh = right>Food</td><td aligh = right>Price</td><td aligh = right>Quantity</td></tr>");
	  			float TotalPrice = 0;
	  			do{
	  				TotalPrice += QueryOrderFormResult.getFloat(6) * QueryOrderFormResult.getInt(7);      				
	  				sBuf.append("<tr><td aligh = right>" + QueryOrderFormResult.getString(5) 
	  							+ "</td><td aligh = right>" + QueryOrderFormResult.getString(6) 
	  							+ "</td><td aligh = right>" + QueryOrderFormResult.getString(7) + "</td></tr>");
	  			}while (QueryOrderFormResult.next()); 
	  			sBuf.append("<tr><td>Total Price : </td><td>" + Float.toString(TotalPrice) + "</td></tr>");
	  		}
	  			
  		}catch (Exception ex) {
			System.out.println("DisplayOrderForm.CheckOrderFormDetails exception");
		}
		return sBuf.toString();
	}
	
	public static String CancelOrderForm(String CancelOrderForm)
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();
		
		String CancelOrderFormStatement = "delete from order_form where form_ID = " + CancelOrderForm;
		sBuf.append(CancelOrderFormStatement);
      	db.prepareStatement(CancelOrderFormStatement);
      	db.updatePstmt();
      	String resultURL = "<script type =\"text/javascript\"> window.location.href = 'customer_account.jsp'</script>";
      	sBuf.append(resultURL);
		
		return sBuf.toString();
	}
}